﻿/*Copyright (C) 2010-2012 RAAFCOM B.V. All rights reserved.

This file is part of Velsto and may be used under the terms of:
The GNU General Public License version 3 or higher, as published by
the Free Software Foundation.

This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.*/
/* 
* Application  : Velsto
* Class        : VswFile
* 04/06/2011   : M.A.van Ravesteijn (Raafcom)
*  06/01/2012    Initial version(v1.0)
* 
* Class with methods to interface SimulationEntity objects with Velsim parameter
* files(vsw files). 
*/
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace Velsto
{
    public class VswFile
    {
        private SimulationEntity se = new SimulationEntity();
        
        public VswFile()
        {
        }

        public SimulationEntity Se
        {
            get { return se; }
            set { se = value; }
        }

        // Method to parse with both decimal separators
        public double parseDoubleString(string sd)
        {
            var currentCulture = System.Globalization.CultureInfo.InstalledUICulture;
            var numberFormat = (System.Globalization.NumberFormatInfo)
            currentCulture.NumberFormat.Clone();
            numberFormat.NumberDecimalSeparator = "."; 
            double number;
            bool succeeded = double.TryParse(sd, System.Globalization.NumberStyles.Any, numberFormat , out number);
            if (succeeded)
            {
                return number;
            }
            else
            {
                numberFormat.NumberDecimalSeparator = ",";
                bool succeeded2 = double.TryParse(sd, System.Globalization.NumberStyles.Any, numberFormat, out number);
                if (succeeded2)
                {
                    return number;
                }
                else
                {
                    return -999.25;
                }
            }
        }


/*
 *  Method to read vswfile(Windows .vsw). 
 *  ArrayLists initialised for size 10.   
 */
        public SimulationEntity readVswfile(string vswFilename) 
        {
/*
 * Read Windows files (.vws)
 */
            if (vswFilename.Contains(".vsw"))
            {
                try
                {
                    string line = null;
                    System.IO.TextReader readFile = new StreamReader(vswFilename);
                    line = readFile.ReadLine();
                    if (line != null) se.Version = line;
                    line = readFile.ReadLine();
                    if (line != null) se.ProjectName = line;
                    line = readFile.ReadLine();
                    if (line != null) se.SimulOption = Convert.ToInt32(line);
                    line = readFile.ReadLine();
                    if (line != null) se.NrLayers = Convert.ToInt32(line);
                    line = readFile.ReadLine();
                    if (line != null) se.TopLayerXtrap = Convert.ToInt32(line);
                    line = readFile.ReadLine();
                    if (line != null) se.ResultFolderpath = line;
                    line = readFile.ReadLine();
                    if (line != null) se.InputFileTWT = line;
                    if (line.Contains("$")) line = readFile.ReadLine();

                    ArrayList alTWT = new ArrayList();
                    for (int i = 0; i < 10; i++)
                    {
                        line = readFile.ReadLine();
                        alTWT.Add(line);
                    }
                    se.AlInputFileTWT = alTWT;
                    ArrayList alV0 = new ArrayList();
                    for (int i = 0; i < 10; i++)
                    {
                        line = readFile.ReadLine();
                        alV0.Add(line);
                    }
                    se.AlInputFileV0 = alV0;

                    ArrayList alF = new ArrayList();
                    for (int i = 0; i < 10; i++)
                    {
                        line = readFile.ReadLine();
                        if (line != null) alF.Add(line);
                    }
                    se.AlInputFileF = alF;

                    ArrayList alVVNdev = new ArrayList();
                    for (int i = 0; i < 10; i++)
                    {
                        line = readFile.ReadLine();
                        if (line != null) alVVNdev.Add(line);
                    }
                    se.AlInputFileVVNdev = alVVNdev;

                    ArrayList alTWTdev = new ArrayList();
                    for (int i = 0; i < 10; i++)
                    {
                        line = readFile.ReadLine();
                        if (line != null) alTWTdev.Add(line);
                    }
                    se.AlInputFileTWTdev = alTWTdev;

                    ArrayList alLP = new ArrayList();
                    char[] delimiterChars = { ' ', '\t' };
                    for (int i = 0; i < se.NrLayers; i++)
                    {
                        LayerparamsEntity lp = new LayerparamsEntity();
                        line = readFile.ReadLine();
                        line = line.Replace(",", ".");
                        string[] words = line.Split(delimiterChars,
                                                     StringSplitOptions.RemoveEmptyEntries);
                        lp.V0vintOption = Convert.ToInt32(words[0]);
                        lp.V0vint = Convert.ToInt32(parseDoubleString(words[1]));
                        lp.V0vintDev = Convert.ToInt32(parseDoubleString(words[2]));
                        lp.K = parseDoubleString(words[3]);
                        lp.KDev = parseDoubleString(words[4]);
                        lp.F = parseDoubleString(words[5]);
                        lp.DepV0K = Convert.ToInt32(words[6]);
                        // convert TWTdev to ms
                        lp.TwtDev = parseDoubleString(words[7])*1000.0;
                        lp.DepVel = Convert.ToInt32(words[8]);
                        alLP.Add(lp);

                    }
                    for (int i = se.NrLayers; i < 10; i++)
                    {
                        LayerparamsEntity lp = new LayerparamsEntity();
                        alLP.Add(lp);
                    }
                    se.AlLayerParams = alLP;
                    if (se.Version.Equals(Constants.VSWVERSION_V12))
                    {
                        ArrayList alLayername = new ArrayList();
                        for (int i = 0; i < 10; i++)
                        {
                            line = readFile.ReadLine();
                            if (line != null) alLayername.Add(line);
                        }
                        se.AlLayername = alLayername;
                    }

                    readFile.Close();
                    readFile = null;
                    se.ErrorMessage = null;
                    return se;
                }
                catch (Exception ex)
                {
                    se.ErrorMessage = ex.ToString();
                    return se;
                }
            }
            else
            {
                se.ErrorMessage = "Input file must have extension vsw";
                return se;
            }
        }

/*
 *  Method to Save SimulationEntity to vswfile
 */
        public string saveVswfile(SimulationEntity se, string vswFilename)
        {
            /*
             * Write vsw file
             */
            if (vswFilename.Contains(".vsw"))
            {
                try
                {
                    TextWriter vswFile = new StreamWriter(vswFilename);
                    vswFile.WriteLine(Constants.VSWVERSION_V12);
                    vswFile.WriteLine(se.ProjectName);
                    vswFile.WriteLine(Convert.ToString(se.SimulOption));
                    vswFile.WriteLine(Convert.ToString(se.NrLayers));
                    vswFile.WriteLine(Convert.ToString(se.TopLayerXtrap));
                    vswFile.WriteLine(se.ResultFolderpath);
                    vswFile.WriteLine(se.InputFileTWT);
                    for (int i = 0; i < 10; i++)
                    {
                        vswFile.WriteLine((String)se.AlInputFileTWT[i]);
                    }
                    for ( int i=0; i<10; i++)
                    {
                        vswFile.WriteLine((String) se.AlInputFileV0[i]);
                    }
                    for ( int i=0; i<10; i++)
                    {
                        vswFile.WriteLine((String) se.AlInputFileF[i]);
                    }
                    for ( int i=0; i<10; i++)
                    {
                        vswFile.WriteLine((String) se.AlInputFileVVNdev[i]);
                    }
                    for ( int i=0; i<10; i++)
                    {
                        vswFile.WriteLine((String) se.AlInputFileTWTdev[i]);
                    }
                        
                    for (int i = 0; i < se.NrLayers; i++)
                    {
                        vswFile.WriteLine(
                        Convert.ToString(((LayerparamsEntity)(se.AlLayerParams[i])).V0vintOption) + " " +
                        ((LayerparamsEntity)(se.AlLayerParams[i])).V0vint.ToString("0.0", CultureInfo.InvariantCulture) + " " +
                        ((LayerparamsEntity)(se.AlLayerParams[i])).V0vintDev.ToString("0.0", CultureInfo.InvariantCulture) + " " +
                        ((LayerparamsEntity)(se.AlLayerParams[i])).K.ToString("0.0000", CultureInfo.InvariantCulture) + " " +
                        ((LayerparamsEntity)(se.AlLayerParams[i])).KDev.ToString("0.0000", CultureInfo.InvariantCulture) + " " +
                        ((LayerparamsEntity)(se.AlLayerParams[i])).F.ToString("0.0000", CultureInfo.InvariantCulture) + " " +
                        Convert.ToString(((LayerparamsEntity)(se.AlLayerParams[i])).DepV0K) + " " +
                        (((LayerparamsEntity)(se.AlLayerParams[i])).TwtDev / 1000.0).ToString("0.0000", CultureInfo.InvariantCulture) + " " +
                        Convert.ToString(((LayerparamsEntity)(se.AlLayerParams[i])).DepVel)
                        );
                    }
                    for (int i = 0; i < se.NrLayers; i++)
                    {
                        vswFile.WriteLine((String)se.AlLayername[i]);
                    }
                    vswFile.Close();
                    return null;
                }
                catch (Exception ex)
                {
                    return ex.ToString();
                }
            }
            else
            {
                return "Filename must have extension .vsw";
            }
        }

        
    }
}
